package school.emm;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ChatRoomTab extends Activity {
	Button btn_;
	TextView text_;
	EditText edit_;
	int i_ = 0;
	private MyListener listener_ = null;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chatroomtab);
        btn_ = (Button) this.findViewById(R.id.test);
        text_ = (TextView) this.findViewById(R.id.chatmessage);
        edit_ = (EditText) this.findViewById(R.id.newMessage);
        
        listener_ = new MyListener();
        registerReceiver(listener_, new IntentFilter("school.emm.ec.NEW_MESSAGE"));
    }
	
	/*
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putString("messages", text_.getText().toString());
	}
	protected void onRestoreInstanceState (Bundle savedInstanceState) {
		text_.setText(savedInstanceState.getString("messages"));
	}
	*/
	public void updateChatText(View v){
		text_.append("You: " + edit_.getText()+"\n");
	    Intent sendMessage = new Intent();
	    sendMessage.setAction("school.emm.ec.SEND_MESSAGE");
	    sendMessage.putExtra("message", edit_.getText().toString());
	    sendBroadcast(sendMessage);
	    edit_.setText("");
    }
	
	public void newChatMessage(String message) {
		text_.append(message + "\n");
		
	}
	
	protected class MyListener extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            // No need to check for the action unless the listener will
            // will handle more than one - let's do it anyway
            if (intent.getAction().equals("school.emm.ec.NEW_MESSAGE")) {
                newChatMessage(intent.getStringExtra("message"));
            }
        }
    }
}
